package com.hjq.fcloud.jump

import com.hjq.fcloud.app.AppActivity
import com.hjq.fcloud.http.api.ProjectImageUploadDetailApi
import com.hjq.fcloud.http.api.ProjectImageUploadDetailBean
import com.hjq.fcloud.http.model.HttpData
import com.hjq.fcloud.ui.activity.CognovitActionemActivity
import com.hjq.fcloud.ui.activity.MaterialsWarehousingConfirmationActivity
import com.hjq.fcloud.ui.activity.PlatformServiceFeeSignActivity
import com.hjq.fcloud.ui.activity.ProjectImageUploadAddActivity
import com.hjq.fcloud.ui.activity.ProjectImageUploadAddEndActivity
import com.hjq.fcloud.ui.activity.ProjectImageUploadAddStartActivity
import com.hjq.fcloud.ui.activity.RegulatoryMaterialConfirmationActivity
import com.hjq.http.EasyHttp
import com.hjq.http.body.JsonBody
import com.hjq.http.listener.HttpCallback

/**
 * 公共列表跳转
 */
object CommonJump {

    const val MAKRER_SPLIT = "FUC_"

    //跳转类型
    const val COMMON_JUMP_CODE_PTFWD = "FUC_57"//平台服务费签约"
    const val COMMON_JUMP_CODE_MWC = "FUC_230"//材料入库确认
    const val COMMON_JUMP_CODE_IMAGE_UPLOAD = "FUC_30002"//形象进度过程登记
    const val COMMON_JUMP_CODE_IMAGE_UPLOAD_START = "FUC_30101"//形象进度起点登记
    const val COMMON_JUMP_CODE_IMAGE_UPLOAD_END = "FUC_30102"//形象进度终点登记


    /**
     * 跳转各个详情
     */
    fun commonJump(context: AppActivity, jumpBean: CommonJumpBean) {
        jumpBean?.let {
            when (jumpBean.originalBizCode) {
                COMMON_JUMP_CODE_PTFWD -> {
                    PlatformServiceFeeSignActivity.start(context, jumpBean)
                }

                COMMON_JUMP_CODE_MWC -> {
                    MaterialsWarehousingConfirmationActivity.start(context, jumpBean)
                }

                COMMON_JUMP_CODE_IMAGE_UPLOAD -> {
                    ProjectImageUploadAddActivity.start(context, jumpBean)
                }

                COMMON_JUMP_CODE_IMAGE_UPLOAD_START -> {
                    checkProjectImageUploadAddOrDetail(
                        context,
                        ProjectImageUploadAddActivity.TYPE_PROGRESS_START,
                        jumpBean
                    ) { isAdd ->
                        if (isAdd) {
                            jumpBean.bizId = null//bizId设为空代表新增
                        }
                        ProjectImageUploadAddStartActivity.start(context, jumpBean)
                    }
                }

                COMMON_JUMP_CODE_IMAGE_UPLOAD_END -> {
                    checkProjectImageUploadAddOrDetail(
                        context,
                        ProjectImageUploadAddActivity.TYPE_PROGRESS_END,
                        jumpBean
                    ) { isAdd ->
                        if (isAdd) {
                            jumpBean.bizId = null//bizId设为空代表新增
                        }
                        ProjectImageUploadAddEndActivity.start(context, jumpBean)
                    }
                }
            }
        }
    }

    /**
     * 判断形象进度是新增还是添加
     */
    private fun checkProjectImageUploadAddOrDetail(
        activity: AppActivity,
        type: Int,
        jumpBean: CommonJumpBean,
        callBack: (isAdd: Boolean) -> Unit
    ) {
        var parms = HashMap<String, Any?>()
        parms.put("fnc_id", jumpBean?.bizId)
        parms.put("type", type)

        EasyHttp.post(activity)
            .api(ProjectImageUploadDetailApi())
            .body(JsonBody(parms))
            .request(object : HttpCallback<HttpData<ProjectImageUploadDetailBean>?>(activity) {
                override fun onSucceed(result: HttpData<ProjectImageUploadDetailBean>?) {
                    var key = ProjectImageUploadAddActivity.KEY_IMAGE_START
                    if (type == ProjectImageUploadAddActivity.TYPE_PROGRESS_END) {
                        key = ProjectImageUploadAddActivity.KEY_IMAGE_END
                    }
                    //图片为空也表示新增
                    var hasImage = result?.getData()?.files?.get(key)?.size!! > 0
                    if(hasImage){
                        callBack(false)
                    }else{
                        callBack(true)
                    }
                }

                override fun onFail(e: java.lang.Exception?) {
                    callBack(true)
                }
            })
    }
}